#include "Login.h"
#include <QMessageBox>
#include <QMainWindow>
Login::Login(QWidget *parent) : QWidget(parent)
{
    // 关键：启用样式表背景绘制
        setAttribute(Qt::WA_StyledBackground, true);
        setAutoFillBackground(true);
        // 直接设置当前部件的背景色（无需类选择器，更直接）
        setStyleSheet("background-color: #2c3e50;");
        initLoginInterface();
}

// ==================== 登录界面初始化 ====================
void Login::initLoginInterface()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 窗口控制按钮区域（右上角）
    QWidget *controlContainer = new QWidget(this);
    controlContainer->setStyleSheet("background:transparent;");
    controlContainer->setFixedHeight(50);
    QHBoxLayout *controlLayout = new QHBoxLayout(controlContainer);
    controlLayout->setContentsMargins(0, 0, 10, 0);
    controlLayout->setAlignment(Qt::AlignRight | Qt::AlignTop);

    QPushButton *btnMinimize = new QPushButton("−", controlContainer);
    btnMinimize->setFixedSize(30, 30);
    btnMinimize->setStyleSheet("QPushButton { background: transparent; color: white; border: none; font-size: 20px; font-weight: bold; }"
                               "QPushButton:hover { background: #5dade2; border-radius: 3px; }");
    connect(btnMinimize, &QPushButton::clicked, [this](){
        if (QMainWindow *mainWin = qobject_cast<QMainWindow*>(parentWidget()->topLevelWidget())) {
            mainWin->showMinimized();
        }
    });

    QPushButton *btnMaximize = new QPushButton("□", controlContainer);
    btnMaximize->setFixedSize(30, 30);
    btnMaximize->setStyleSheet("QPushButton { background: transparent; color: white; border: none; font-size: 16px; }"
                               "QPushButton:hover { background: #5dade2; border-radius: 3px; }");
    connect(btnMaximize, &QPushButton::clicked, [this, btnMaximize]() {
        QMainWindow *mainWin = qobject_cast<QMainWindow*>(parentWidget()->topLevelWidget());
        if (!mainWin) return;

        if (mainWin->isMaximized()) {
            mainWin->showNormal();
            btnMaximize->setText("□");
        } else {
            mainWin->showMaximized();
            btnMaximize->setText("◫");
        }
    });

    QPushButton *btnClose = new QPushButton("×", controlContainer);
    btnClose->setFixedSize(30, 30);
    btnClose->setStyleSheet("QPushButton { background: transparent; color: white; border: none; font-size: 20px; font-weight: bold; }"
                            "QPushButton:hover { background: #e74c3c; border-radius: 3px; }");
    connect(btnClose, &QPushButton::clicked, [this](){
        parentWidget()->topLevelWidget()->close();
    });

    controlLayout->addWidget(btnMinimize);
    controlLayout->addWidget(btnMaximize);
    controlLayout->addWidget(btnClose);

    // 登录表单容器（居中）
    QWidget *formContainer = new QWidget(this);
    formContainer->setStyleSheet("background:transparent;");
    QVBoxLayout *formLayout = new QVBoxLayout(formContainer);
    formLayout->setSpacing(30);
    formLayout->setContentsMargins(100, 50, 100, 80); // 增加左右边距，减少上下边距

    // 登录标题
    m_labLoginTitle = new QLabel("用户登录", formContainer);
    m_labLoginTitle->setStyleSheet("color:white; font-size:32px; font-weight:bold; margin-bottom: 20px;");
    m_labLoginTitle->setAlignment(Qt::AlignCenter);

    // 用户名输入行 - 使用网格布局更好对齐
    QWidget *userWidget = new QWidget(formContainer);
    QHBoxLayout *userLayout = new QHBoxLayout(userWidget);
    userLayout->setSpacing(15);
    userLayout->setContentsMargins(0, 0, 0, 0);

    m_labLoginUser = new QLabel("用户名：", userWidget);
    m_labLoginUser->setStyleSheet("color:white; font-size:18px; min-width: 80px;");
    m_labLoginUser->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_edtLoginUser = new QLineEdit(userWidget);
    m_edtLoginUser->setStyleSheet("background:white; color:#333; font-size:16px; padding:12px; border:2px solid #bdc3c7; border-radius:8px;"
                                  "QLineEdit:focus { border-color: #3498db; }");
    m_edtLoginUser->setMinimumHeight(45);
    m_edtLoginUser->setPlaceholderText("请输入用户名");
    m_edtLoginUser->setStyleSheet("background:white;");

    userLayout->addWidget(m_labLoginUser);
    userLayout->addWidget(m_edtLoginUser, 1); // 输入框占据剩余空间

    // 密码输入行
    QWidget *pwdWidget = new QWidget(formContainer);
    QHBoxLayout *pwdLayout = new QHBoxLayout(pwdWidget);
    pwdLayout->setSpacing(15);
    pwdLayout->setContentsMargins(0, 0, 0, 0);

    m_labLoginPwd = new QLabel("密码：", pwdWidget);
    m_labLoginPwd->setStyleSheet("color:white; font-size:18px; min-width: 80px;");
    m_labLoginPwd->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_edtLoginPwd = new QLineEdit(pwdWidget);
    m_edtLoginPwd->setEchoMode(QLineEdit::Password);
    m_edtLoginPwd->setStyleSheet("background:white; color:#333; font-size:16px; padding:12px; border:2px solid #bdc3c7; border-radius:8px;"
                                 "QLineEdit:focus { border-color: #3498db; }");
    m_edtLoginPwd->setMinimumHeight(45);
    m_edtLoginPwd->setPlaceholderText("请输入密码");
    m_edtLoginPwd->setStyleSheet("background:white;");

    pwdLayout->addWidget(m_labLoginPwd);
    pwdLayout->addWidget(m_edtLoginPwd, 1);

    // 按钮区 - 使用网格布局让按钮居中
    QWidget *btnWidget = new QWidget(formContainer);
    QHBoxLayout *btnLayout = new QHBoxLayout(btnWidget);
    btnLayout->setSpacing(20);
    btnLayout->setContentsMargins(0, 20, 0, 0);
    btnLayout->setAlignment(Qt::AlignCenter);

    m_btnLogin = new QPushButton("登录", btnWidget);
    m_btnLogin->setStyleSheet("QPushButton {"
                              "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #3498db, stop:1 #2980b9);"
                              "color:white; font-size:18px; font-weight:bold; padding:12px 40px;"
                              "border:none; border-radius:8px; min-width: 120px;"
                              "}"
                              "QPushButton:hover { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #2980b9, stop:1 #3498db); }"
                              "QPushButton:pressed { background: #21618c; }");
    m_btnLogin->setFixedHeight(50);

    m_btnToRegister = new QPushButton("注册新用户", btnWidget);
    m_btnToRegister->setStyleSheet("QPushButton {"
                                   "background: #ecf0f1; color:#2c3e50; font-size:18px; font-weight:bold; padding:12px 30px;"
                                   "border:2px solid #bdc3c7; border-radius:8px; min-width: 120px;"
                                   "}"
                                   "QPushButton:hover { background: #d5dbdb; border-color: #95a5a6; }"
                                   "QPushButton:pressed { background: #bfc9ca; }");
    m_btnToRegister->setFixedHeight(50);

    btnLayout->addWidget(m_btnLogin);
    btnLayout->addWidget(m_btnToRegister);

    // 组装表单
    formLayout->addStretch(1);
    formLayout->addWidget(m_labLoginTitle);
    formLayout->addWidget(userWidget);
    formLayout->addWidget(pwdWidget);
    formLayout->addWidget(btnWidget);
    formLayout->addStretch(2);

    // 组装主布局
    mainLayout->addWidget(controlContainer);
    mainLayout->addWidget(formContainer, 1); // 表单容器占据剩余空间

    // 绑定槽函数
    connect(m_btnLogin, &QPushButton::clicked, this, &Login::on_btnLogin_clicked);
    connect(m_btnToRegister, &QPushButton::clicked, this, &Login::on_btnToRegister_clicked);

}

void Login::on_btnLogin_clicked()
{
    if (!checkLoginInput()) return;

    QString username = m_edtLoginUser->text().trimmed();
    QString password = m_edtLoginPwd->text().trimmed();

    //发送登录请求信号，由主窗口处理验证逻辑
    emit loginRequested(username,password);
}

void Login::on_btnToRegister_clicked()
{
    // 发送切换到注册界面的信号
    emit switchToRegister();
    // 清空输入框
    m_edtLoginUser->clear();
    m_edtLoginPwd->clear();
}
bool Login::checkLoginInput()
{
    QString username = m_edtLoginUser->text().trimmed();
    QString password = m_edtLoginPwd->text().trimmed();

    if (username.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入用户名！");
        m_edtLoginUser->setFocus();
        return false;
    }
    if (password.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入密码！");
        m_edtLoginPwd->setFocus();
        return false;
    }
    return true;
}
// 新增：设置用户名到输入框
void Login::setUsername(const QString &username)
{
    if (m_edtLoginUser) {
        m_edtLoginUser->setText(username);
        m_edtLoginPwd->setFocus();  // 光标自动定位到密码框
    }
}
